<?php

/**
 * Description of EmptyAction
 *
 * @author chacha
 * @date 2015-12-19
 */
class EmptyAction extends BaseAction{
    
    public function _empty() {
        $this->Page404();
    }
    
    public function index() {
        $name = MODULE_NAME;
        $this->outlist($name);
    }
    
    protected function outlist($name) {
        if ($name=='projects') {
            $this->projects();
        }
        else if ($name=='talents') {
            $this->talents();
        }
        else if(preg_match("/job\d+/", $name)){
            $jobid = str_replace('job', '', $name);
            Log::write("[EmptyAction][job]".$jobid);
            $this->jobdetail($jobid);
        }
        else if (preg_match("/talent\d+/", $name)) {
            $uid = str_replace('talent', '', $name);
            Log::write("[EmptyAction][talent]".$uid);
            $this->talentdetail($uid);
        }
        else if (preg_match("/Cop\d+/", $name)) {
            $copid = str_replace('Cop', '', $name);
            require APP_PATH.'Group/Bridge/Lib/Info/Cop.class.php';
            $CopAction = new \Lib\Info\Cop();
            if (method_exists($CopAction, 'info')) {
                $CopAction->info($copid);
            }
            else {
                $this->Page404();
            }
        }
        else if ($name=='aboutus'
                || $name=='conditions'
                || $name=='privacy'
                || $name=='statement'
                || $name=='faq'
                || $name=='contactus'
                || $name=='userrules'
                || $name=='questions') 
        {
            $tpl = "../Include/App/Group/Bridge/Tpl/Index_".$name.".html";
            $this->display($tpl);
        }
        else {
            $this->Page404();
        }
    }
    
    protected function projects() {
        $cdate = I('timegap', 0);
        $sortype = I('sortype', 1);
        $prj = M('project')->order(array('ctime'))->limit(1)->select();
        $project = null;
        if ($prj) {
            $startime = 0;
            if ($cdate == 1) {
                $startime = $prj['ctime'] - 86400 * 7;
            }
            if ($cdate == 2) {
                $startime = $prj['ctime'] - 86400 * 30;
            }
            //分页查询
            $order = '';
            if ($sortype==1) {//已托管
                $order = '(case job.moneyin when 1 then 4 when 4 then 3 when 2 then 2 when 0 then 1 end) desc, job.ctime';
            }
            else if ($sortype==2) {//职位数最多
                $order = 'job.num-job.datastate desc, job.ctime';
            }
            else if ($sortype==3) {//薪酬最高
                $order = 'job.salary desc, job.ctime';
            }
            
            $tables = array('xia_job'=>'job', 'xia_project'=>'prj', 'xia_team'=>'team', 'xia_user_info'=>'uif', 'xia_account'=>'ac');//array('xia_project' => 'prj', 'xia_team' => 'team');
            $field = 'job.*,prj.id as pid,prj.name as pname,team.name as tname,uif.nickname as creater, ac.emailverify,ac.passcheck';
            $where = 'job.state>0 and job.ctime>'.$startime.' and job.state>0 and job.pid=prj.id and prj.state>0 and prj.tid=team.id and team.power=2 and team.uid=uif.uid and uif.uid=ac.uid and ac.passcheck<>0';
            import('ORG.Util.Page');// 导入分页类
            $count      = M('project')->table($tables)->field($field)->where($where)->order('job.ctime desc')->count();// 查询满足要求的总记录数
            $Page       = new Page($count,20);// 实例化分页类 传入总记录数
            // 进行分页数据查询 注意page方法的参数的前面部分是当前的页数使用 $_GET[p]获取
            $nowPage = isset($_GET['p'])?$_GET['p']:1;
            $pageField = $field.",(SELECT COUNT(*) FROM xia_jobfav jf WHERE job.id=jf.jobid) as fc";
            $resJobs = M('job')->table($tables)->field($pageField)->where($where)->page($nowPage.','.$Page->listRows)->order($order)->select();
            $show    = $Page->show();// 分页显示输出
            $this->assign('page',$show);// 赋值分页输出
            $resMfav = M('jobfav')->where('uid='.$this->uid)->select();
            $jobs = array();
            foreach ($resJobs as $job) {
                $job['faved'] = 0;
                if ($resMfav) {
                    foreach ($resMfav as $fav) {
                        if ($fav['jobid']==$job['id']) {
                            $job['faved'] = 1;
                        }
                    }
                }
                
                $job['creater'] = convEmail($job['creater']);
                $jobs[] = $job;
            }
        }
//        dump($jobs);
//        exit;
        $this->assign("project", $jobs);
        $this->assign('tip', C('PRJ_MONEY_TUOGUAN_TIP_BROWSER'));
        $this->assign("on", 2);
        $this->assign('timegap', $cdate);
        $this->assign('sortype', $sortype);
        $this->display("../Include/App/Group/Bridge/Tpl/Index_projects.html");
    }
    
    protected function talents() {
        $cdate = I('timegap', 0);
        $startime = 0;
        $sortype = I('sortype', 2);//默认按工作资历排序
        if ($cdate == 1) {
            $startime = time() - 86400 * 7;
        }
        if ($cdate == 2) {
            $$startime = time() - 86400 * 30;
        }
        if ($sortype==1) {
            $order = "xia_user_info.teamid, xia_account.infopers desc, xia_user_info.uptime desc";
        }
        else if ($sortype==2) {
            $order = "xia_user_info.exp desc, xia_account.infopers desc, xia_user_info.uptime desc";
        }
        
        $table = 'xia_user_info,xia_account';
        $where = "xia_user_info.utype=1 and xia_user_info.uptime>' . $startime . ' and xia_user_info.uid=xia_account.uid and xia_account.passcheck=1 and xia_account.emailverify=1";
        import('ORG.Util.Page');// 导入分页类
        $count      = M('user_info')->table($table)->where($where)->count();// 查询满足要求的总记录数
        $Page       = new Page($count,20);// 实例化分页类 传入总记录数
        // 进行分页数据查询 注意page方法的参数的前面部分是当前的页数使用 $_GET[p]获取
        $nowPage = isset($_GET['p'])?$_GET['p']:1;
        $rencai = M('user_info')->table($table)->where($where)->page($nowPage.','.$Page->listRows)->order($order)->select();
        //隐私过滤
        $myinfo = $this->userinfo;
        for ($i=0;$i<count($rencai);$i++) {
            userInfoFilter($myinfo, $rencai[$i]);
            $rencai[$i]['nickname'] = convEmail($rencai[$i]['nickname']);
        }

        $show       = $Page->show();// 分页显示输出
        $this->assign('page',$show);// 赋值分页输出
        
        $this->assign('timegap', $cdate);
        $this->assign('sortype', $sortype);
        $this->assign("rencai", $rencai);
        $this->assign("on", 3);
        $this->display("../Include/App/Group/Bridge/Tpl/Index_talents.html");
    }
    
    //职位详情页
    public function jobdetail($jobid=1) {
        $id = $jobid;
        if ($id==1) {
            $this->redirect('/Index/projects@'.getDomain());
        }
        $prjTable = array('xia_project' => 'arr', 'xia_team' => 'team', 'xia_user_info'=>'uif');
        $prjField = 'arr.*,team.name as tname,team.logo,team.weibo,team.weixin,uif.nickname as creater';
        $prjWhere = 'arr.id=' . $id . ' and arr.tid=team.id and team.uid=uif.uid';
        $prjInfo = M('project')->table($prjTable)->field($prjField)->where($prjWhere)->find();
        $julTable = array('xia_job_user' => 'list', 'xia_job' => 'job', 'xia_user_info' => 'user');
        $julField = 'list.*,job.name as jname,user.nickname,user.photo';
        $julWhere = 'list.pid=' . $id . ' and list.jobid=job.id and job.state>0 and list.uid=user.uid and (list.state=2 or list.state=4 or list.state=5)';
        $jobUsers = M("job_user")->table($julTable)->field($julField)->where($julWhere)->select();
        $jlField = "j.*,(select COUNT(*) from xia_jobfav jf where j.id=jf.jobid) as fc,(select COUNT(*) from xia_job_user ju where j.id=ju.jobid and (ju.state=0 or ju.state=2)) as aplc";
        $order = "(case j.moneyin when 1 then 4 when 4 then 3 when 2 then 2 when 0 then 1 end) desc, (j.num-j.datastate) desc, j.ctime";
        $jobs = M('job')->table(array('xia_job'=>'j'))->field($jlField)->where('j.pid=' . $id. ' and j.state>0')->order($order)->select();//职位统计
        //$jmFiled = "j.id,(select COUNT(*) from xia_jobfav if where j.id=jf.jobid and jf.uid=".$this->uid.")";
        $resMfav = M()->table(array('xia_jobfav'=>'jf'))->where('jf.uid=' . $this->uid)->select();//当前用户收藏的
        $jobTotal = M('job')->where('pid=' . $id.' and state>0')->sum('num');
        if (!$jobTotal) {
            $jobTotal = 0;
        }
        $rest = $jobTotal - count($jobUsers);
        $rest = ($rest >= 0) ? $rest : 0;
        
        $alljobs = array();
        //已申请
        $applieds = M('job_user')->where('uid='.$this->uid.' and state=0')->select();
        foreach ($jobs as $i=>$job) {
            $job['applied'] = 0;
            foreach ($applieds as $applied) {
                if ($job['id']==$applied['jobid']) {
                    $job['applied'] = 1;
                }
            }
            $job['faved'] = 0;
            foreach ($resMfav as $fav) {
                if ($job['id']==$fav['jobid']) {
                    $job['faved'] = 1;
                }
            }
            //取分期薪酬数据
            if ($job['instalment']==2) {
                $instals = M('instal_stage')->where("jobid=".$job['id'])->select();
                $job['instals'] = $instals;
            }
            $alljobs[] = $job;
        }
        
//        dump($alljobs);
//        exit;
        
        //屏蔽创建者email
        $prjInfo['creater'] = convEmail($prjInfo['creater']);
        
        $this->assign("prjInfo", $prjInfo);
        $this->assign("jobUsers", $jobUsers);
        $this->assign("pid", $id);
        $this->assign("uid", $this->uid);
        $this->assign("jobTotal", $jobTotal);
        $this->assign('countData', count($jobUsers));
        $this->assign("rest", $rest);
        $this->assign("jobs", $alljobs);
        $this->assign('tip', C('PRJ_MONEY_TUOGUAN_TIP_BROWSER'));
        $this->assign("on", 2);
        $this->display("../Include/App/Group/Bridge/Tpl/Job_index.html");
    }
    
    //人才介绍页
    private function isTeamCreater($uid, $tid){
        $resTeam = M('team')->where('id='.$tid.' and state>0')->find();
        if ($resTeam && $resTeam['uid']==$uid) {
            return true;
        }
        return false;
    }
    
    private function isPrjCreater($uid, $pid){
        $resPrj = M('project')->where('id='.$pid.' and state>0')->find();
        if ($resPrj && $resPrj['uid']==$uid) {
            return true;
        }
        return false;
    }
    
    protected function sidebarData($uid) {
        $arr = M('user_info')->field('xia_user_info.*, xia_dic.name as goodat')->where("xia_user_info.uid ='" . $uid . "'")->join("xia_dic ON xia_user_info.shanchang=xia_dic.id")->find();
        //数据过滤
        if ($arr['mobile'] > 0) {
            if (
                    //$this->userinfo['utype'] == 2 //企业用户也不能看到，为vip设计预留
                    (($this->userinfo['teamid'] > 0) && $this->userinfo['teamid'] == $arr['teamid']) //在同一个团队
            )
            {
            }
            else{
                $arr['mobile'] = hidetel($arr['mobile']); //对号码加星号处理
            }
        }
        //隐私过滤
        $this->priArr = userInfoFilter($this->userinfo, $arr);
        $arr['nickname'] = convEmail($arr['nickname']);
        $this->assign("theirinfo", $arr); //用户信息
        
        //抛橄榄枝数据
        //自己所在团队的空缺职位
        $resJob = M('job')->field('id,name')->where('pid='.$this->userinfo['projectid'].' and num>datastate and moneyin<>2 and state>0')->select();
        if ($resJob) {
            $this->assign('jobs', $resJob);
            
        }
        //flag，0可抛，1对方与自己在同一个团队，2对方已在别的团队，3自己无权限邀请，4对方账户未通过人工审核不能被邀请，5自己没有团队
        $resAccount = M('account')->where('uid='.$uid)->find();
        $this->assign('account', $resAccount);
        $flag = 0;
        if ((($this->userinfo['teamid'] > 0 || $arr['teamid'] > 0) && $this->userinfo['teamid'] == $arr['teamid'])) {
            $flag = 1;
        }
        else if ($this->userinfo['teamid']<=0) {
            $flag = 5;
        }
        else if ($arr['teamid'] > 0) {
            $flag = 2;
        }
        else if ($this->uid>0 && !$this->isTeamCreater($this->uid, $this->userinfo['teamid']) && !$this->isPrjCreater($this->uid, $this->userinfo['projectid'])) {
            $flag = 3;
        }
        else if ($resAccount['passcheck']==0) {
            $flag = 4;
        }
        $this->assign('iflag', $flag);
        
        //评价数据
        $tabUC = M('usercomment');
        $rateArr = $tabUC->query("SELECT ROUND(AVG(rate1)) AS avg1,ROUND(AVG(rate2)) AS avg2,ROUND(AVG(rate3)) AS avg3,ROUND(AVG(rate4)) AS avg4 FROM xia_usercomment WHERE ct=2 and uid=".$uid);
        //好评率,大于7分算好评
        $total = $tabUC->where("ct=2 and uid=".$uid)->count();
        $w1 = $tabUC->where("ct=2 and rate1>=7 and uid=".$uid)->count();
        $w2 = $tabUC->where("ct=2 and rate2>=7 and uid=".$uid)->count();
        $w3 = $tabUC->where("ct=2 and rate3>=7 and uid=".$uid)->count();
        $w4 = $tabUC->where("ct=2 and rate4>=7 and uid=".$uid)->count();
        $wellrate = floor((($w1+$w2+$w3+$w4)/($total*4))*100);
        
        $this->assign('wellrate', $wellrate);
        $this->assign('ratearr', $rateArr[0]);
    }
    
    public function talentdetail($uid=0) {
        if ($uid==0) {
            $this->redirect('/Index/talents@'.getDomain());
        }
        $this->sidebarData($uid);

        //要求权限
        $invitAuth = 1;
        $resTeam = M('team')->where('uid=' . $this->uid . ' and state>0')->find();
        if (!$resTeam) {
            $resPrj = M('project')->where('uid=' . $this->uid)->find();
            if (!$resPrj) {
                $resAuth = M('authdispatch')->where('arid=' . $this->userinfo['role'] . ' and authid=52')->select();
                if (!$resAuth) {
                    $invitAuth = 0;
                }
            }
        }
        
        //该用户是否已有团队
        $resUserinfo = M('user_info')->where('uid=' . $uid)->find();
        if ($resUserinfo['teamid'] > 0) {
            $invitAuth = 2;
        }
        $this->assign('f_jianli', $this->priArr['f_jianli']);
        $arrjl = M('jianli')->where("uid ='" . $uid . "'")->order('id desc')->select();
        $jianli_results=array();
        if ($arrjl) {
            foreach($arrjl as $item){
                $row = $item;
                $time=explode(',',$item["time"]);
                $row["btime"]=$time[0];
                $row['etime']=$time[1];
                $jianli_results[]=$row;
            }
            $this->assign("arrjl", $jianli_results); //履历
        }
        
        $this->assign('me', $this->uid);
        $this->assign("userid", $uid);
        $this->assign('invitauth', $invitAuth);
        $this->assign('headnav', 1);
        $this->assign("on", 3);
        $this->display('../Include/App/Group/Bridge/Tpl/Talent_index.html');
    }
}
